package persistence;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import model.Payee;

public class PayeeDAO extends BaseDAO {
	
	public PayeeDAO() throws Exception{}
	
	public int deleteAllPayee() throws SQLException{
		int retorno;
		String sql = "DELETE FROM payee";
		PreparedStatement ps = this.getConnection().prepareStatement(sql);
		retorno = ps.executeUpdate();
		closeConnection();
		return retorno;  
	}
	
	public boolean insertPayee(Payee payee, boolean closeConnection) throws SQLException{
		boolean retorno;
		String sql = "INSERT INTO payee(_id, title, last_category_id, updated_on) values (?,?,?,?)";
		PreparedStatement ps = this.getConnection().prepareStatement(sql);
		ps.setInt    (1, payee.getId());
		ps.setString (2, payee.getTitle());
		ps.setInt    (3, payee.getLastCategoryId());
		ps.setLong   (4, payee.getUpdatedOn());
		retorno = ps.executeUpdate() == 1;
	    if(closeConnection){
	    	closeConnection();
	    }
	    return retorno;
	}
	
	public int insertListPayee(List<Payee> listPayee) throws Exception{
		int ret=0;
		for (int i = 0; i < listPayee.size(); i++) {
			boolean foiInserido;
			foiInserido = insertPayee(listPayee.get(i),false);
			if(foiInserido){
				ret++;
			}
		}
		closeConnection();
		return ret;
	}
}
